/**
 * 弹出功能窗口：同时处理多个数据模块操作<br>
 * 例如：在录入客户跟踪的同时，又需要同时输入新的“首次跟踪记录”，这时就需要不关闭客户跟踪的同时，进行首次跟踪
 */

// 模块窗口列表
var __wins;

//页面载入初始化
$( "body" ).ready( function( e )
{
	__wins = [];
} );

//窗口大小改变
$( window ).resize( function( e )
{
} );

/**
 * 创建并打开新的模块窗口
 * 
 * @param url 模块窗口所链接的网页地址
 * @param title 模块窗口的标题
 * @param mid 模块窗口的ID，不设置则使用随机ID
 * @param idx 窗口弹出层次
 */
function create_model( url, title, mid, idx, closeHandler )
{
	// 若存在同名则不再创建
	for( var i = 0; i < __wins.length; i++ )
	{
		if( i=>0 )
		{
			__wins[ i ].closeHandler = closeHandler;
			return false;
		}
	}
	
	// 不存在，则创建新窗口
	var mw = new ModelWindow( url, title, mid, idx, closeHandler );
	var id = mw.init();
	__wins.push( mw );
	return id;
}

function open_model( id, showMask )
{
	for( var i = 0; i < __wins.length; i++ )
	{
		if( __wins[ i ].id() == id )
		{
			__wins[ i ].open();
			__wins[ i ].mask( showMask );
			return;
		}
	}
}

function close_model( id )
{
	for( var i = 0; i < __wins.length; i++ )
	{
		if( __wins[ i ].id() == id )
		{
			__wins[ i ].close();
			document.getElementById('content_container').contentWindow.location.reload(true);
		}
	}
}

function remove_model( mod )
{
	for( var i = 0; i < __wins.length; i++ )
	{
		if( __wins[ i ] == mod )
		{
			__wins.splice( i, 1 );
			return;
		}
	}
}

function layer_model( mod )
{
	if( mod.index() == __wins.length + 99 || mod.fixed() )
		return;

	mod.index( __wins.length + 99 );

	for( var i = __wins.length - 1; i >= 0; i-- )
	{
		if( __wins[ i ] != mod )
		{
			__wins[ i ].index( i );
		}
	}
}

function move_model( id, x, y )
{
	for( var i = 0; i < __wins.length; i++ )
	{
		if( __wins[ i ].id() == id )
		{
			__wins[ i ].relocate( x, y );
			return;
		}
	}
}

/**
 * 弹出窗口页面内部点击时提高到最上层
 * 
 * @param id 弹出窗口的ID
 */
function layer_call( id )
{
	for( var i = 0; i < __wins.length; i++ )
	{
		if( __wins[ i ].id() == id && !__wins[ i ].fixed() )
		{
			layer_model( __wins[ i ] );
			return;
		}
	}
}

function ModelWindow( url, title, mid, idx, closeHandler )
{
	this.closeHandler = closeHandler;

	var _url = url;
	var _title = title;
	var _mid = mid;
	var html = "<div id='{id}' class='model_window' style='position:absolute;'>"
			+ "<dt><span id='title' contenteditable='true'>{title}</span><a id='btn_cancel'>[ 关闭 ]</a><a id='btn_minimize'>[ 最小化 ]</a><a id='btn_height'>[ 加高 ]</a><a id='btn_width'>[ 加宽 ]</a></dt>"
			+ "<div id='body' style='height:auto;overflow:hidden;'><iframe name='frm_{id}' width='100%' height='100%' frameborder='0'></iframe></div>"
			+ "<div id='mover'></div>" + "</div>";

	var $box;
	var $body;
	var $head;
	var $frm;
	var $btnMini;
	var _index = idx;// 设置了层次的弹出窗口都作为固定层
	var _isShowMask;
	var _fixed = false;

	var _moveable = false;

	this.id = function()
	{
		return _mid;
	};

	this.fixed = function()
	{
		return _fixed;
	};

	this.init = function()
	{
		if( typeof ( _mid ) == "undefined" || _mid == null )
			_mid = hash();
		html = html.replace( /\{id\}/ig, _mid );
		html = html.replace( /\{title\}/ig, _title );

		$( "body" ).append( html );
		$box = $( "#" + _mid );
		$body = $box.find( "#body" );
		$frm = $body.find( ":first" );

		if( empty( _index ) )
		{
			_fixed = false;
			this.index( __wins.length + 99 );
		}
		else
		{
			_fixed = true;
			this.index( _index );
		}

		$box.click( {
			cur: this
		}, function( e )
		{
			layer_model( e.data.cur );
		} );

		// 窗口移动
		var mdown = function( e )
		{
			_moveable = true;

			// 鼠标相对于当前窗口的位置
			var offsetX = e.pageX - $box.x();
			var offsetY = e.pageY - $box.y();

			$( "body" ).bind( "mousemove", {
				ox: offsetX,
				oy: offsetY
			}, function( e )
			{
				if( _moveable )
				{
					$box.move( e.pageX - e.data.ox, e.pageY - e.data.oy );
				}
			} );
		};

		var mup = function( e )
		{
			_moveable = false;
			$( "body" ).unbind( "mousemove" );
		};

		$head = $box.find( "dt" );
		$head.mousedown( mdown );
		$head.mouseup( mup );

		$mover = $box.find( "#mover" );
		$mover.mousedown( mdown );
		$mover.mouseup( mup );

		$head.dblclick( {
			cur: this
		}, function( e )
		{
			e.stopPropagation();
			var pcur = e.data.cur;
			if( $btnMini.text() == "[ 还原 ]" )
			{
				pcur.restore();
			}
			else
			{
				pcur.minimize();
			}
		} );

		$box.find( "#btn_cancel" ).click( {
			cur: this
		}, function( e )
		{
			e.stopPropagation();
			var pcur = e.data.cur;
			jconfirm( "关闭窗口将丢失未保存的修改内容，确定关闭吗？", "关闭确认", function( r )
			{
				if( r )
				{
					pcur.close();
				}
			} );
		} );

		$btnMini = $box.find( "#btn_minimize" );
		$btnMini.click( {
			cur: this
		}, function( e )
		{
			e.stopPropagation();
			var pcur = e.data.cur;

			if( $btnMini.text() == "[ 还原 ]" )
			{
				pcur.restore();
			}
			else
			{
				pcur.minimize();
			}

		} );

		$box.find( "#btn_width" ).click( function( e )
		{
			e.stopPropagation();
			$box.width( $box.width() + 50 );
			$frm.width( $body.width() );
		} );

		$box.find( "#btn_height" ).click( function( e )
		{
			e.stopPropagation();
			$box.height( $box.height() + 50 );
			$body.height( $box.height() - 40 );
			$frm.height( $body.height() );
		} );

		$box.hide();
		this.relocate();
		this.resize();

		return _mid;
	};

	this.index = function( idx )
	{
		if( typeof ( idx ) == "undefined" )
			return _index;

		_index = idx;
		$box.css( "z-index", _index );
	};

	this.minimize = function()
	{
		$body.hide();
		$body.attr( "rel", $body.height() );
		$box.height( $head.height() );
		$btnMini.text( "[ 还原 ]" );
	};

	this.restore = function()
	{
		$body.show();
		$box.height( $head.height() + parseInt( $body.attr( "rel" ) ) );
		$btnMini.text( "[ 最小化 ]" );
	};

	// 显示单条信息
	this.open = function( url )
	{
		this.relocate();
		$box.fadeIn( 150 );

		var u;

		if( typeof ( url ) != "undefined" )
			u = url;
		else
			u = _url;

		u = url_mid( u );
		$frm.attr( "src", u );
	};

	this.mask = function( show )
	{
		if( show == true || show == 1 )
		{
			_isShowMask = true;

			if( typeof ( mask_layer ) != "undefined" )
				mask_layer.open();
		}
		else
		{
			_isShowMask = false;
		}
	};

	function url_mid( url )
	{
		if( url.indexOf( "id=" ) != -1 )
			return url;

		// url 添加 mid 传值，以提供能点击页面时将窗口提到最前
		if( url.indexOf( "?" ) == -1 )
		{
			url += "";
		}
		else
		{
			url += "";
		}

		return url;
	}

	this.resize = function()
	{
		var h = $box.height();
		var headHeight = $head.height();
		$body.height( h - headHeight );
	};

	this.relocate = function( x, y )
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		if( empty( x ) && x != 0 )
			x = ( bw - w ) / 2;
		if( empty( y ) && y != 0 )
			y = ( bh - h ) / 2 * 0.8;

		$box.move( x, y );
	};

	this.close = function()
	{
		remove_model( this );
		$box.hide();
		this.destroy();

		if( typeof ( this.closeHandler ) == "function" )
			this.closeHandler();

		if( typeof ( mask_layer ) != "undefined" && _isShowMask )
			mask_layer.close();
	};

	this.destroy = function()
	{
		$head.unbind( "mousedown" );
		$head.unbind( "mouseup" );
		$box.unbind( "click" );
		$( "body" ).unbind( "mousemove" );
		$box.find( "#btn_cancel" ).unbind( "click" );
		$frm.attr( "src", "" );
		$box.remove();
	};
}
